﻿using System;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using GatewayInspect.Common;

namespace GatewayInspect.Model
{
    /// <summary>
    /// 车辆信息
    /// </summary>
    public class TruckInfo
    {
        public TruckInfo()
        {
            this.CYZGZ_JSYZ = new YSJSZ();
            this.CYZGZ_YYYZ = new YSYYRYZ();
        }

        #region Propertys

        /// <summary>
        /// 主键
        /// </summary>
        [Browsable(false)]
        public string ID { get; set; }

        /// <summary>
        /// 车牌号
        /// </summary>
        [Category("\t\t\t\t\t基本信息"), DisplayName("车牌号")]
        public string TRUCKNO { get; set; }

        [Category("\t\t\t\t\t基本信息"), DisplayName("车辆载质量(吨)")]
        public double HD_WEIGHT { get; set; }

        /// <summary>
        /// 司机姓名
        /// </summary>
        [Category("\t\t\t\t\t基本信息"), DisplayName("司机姓名")]
        public string DRIVERNAME { get; set; }

        /// <summary>
        /// 司机身份证号
        /// </summary>
        [Category("\t\t\t\t\t基本信息"), DisplayName("司机身份证号")]
        public string DRIVERID { get; set; }

        [Category("\t\t\t\t\t基本信息"), DisplayName("灭火器有效期")]
        public DateTime MHQ_YXQ { get; set; }

        [Category("\t\t\t\t驾驶证"), DisplayName("姓名")]
        public string JSZ_NAME { get; set; }

        [Category("\t\t\t\t驾驶证"), DisplayName("驾驶证号")]
        public string JSZ_NO { get; set; }

        [Category("\t\t\t\t驾驶证"), DisplayName("驾驶证有效期")]
        public DateTime JSZ_YXQ { get; set; }

        [Category("\t\t\t车辆行驶证"), DisplayName("检验有效期(车头)")]
        public DateTime XSZ_CT_YXQ { get; set; }

        [Category("\t\t\t车辆行驶证"), DisplayName("检验有效期(车挂)")]
        public DateTime XSZ_CG_YXQ { get; set; }

        [Category("\t\t中华人民共和国道路运输证"), DisplayName("车辆审验及技术等级记录(车头)")]
        public DateTime YSZ_CT_SYJL { get; set; }

        [Category("\t\t中华人民共和国道路运输证"), DisplayName("车辆审验及技术等级记录(车挂)")]
        public DateTime YSZ_CG_SYJL { get; set; }

        [Category("\t中华人民共和国道路运输从业资格证"), DisplayName("运输驾驶员证"), TypeConverter(typeof(SimpleConverter))]
        public YSJSZ CYZGZ_JSYZ { get; set; }

        [Category("\t中华人民共和国道路运输从业资格证"), DisplayName("运输押运人员证"), TypeConverter(typeof(SimpleConverter))]
        public YSYYRYZ CYZGZ_YYYZ { get; set; }

        [Category("道路运输液体危险货物罐式车辆常压罐体定期检验报告"), DisplayName("有效期")]
        public DateTime JYBG_YXQ { get; set; }

        [Category("道路运输液体危险货物罐式车辆常压罐体定期检验报告"), DisplayName("检验单位")]
        public string JYBG_JYDW { get; set; }

        #endregion

        #region static Methods

        public static TruckInfo GetInstanceFromDB(string id)
        {
            DataTable dtInfo = DataAccess.ExecuteDataTable("select * from T_TRUCK_INFO where ID = '" + id + "'");
            if (dtInfo != null && dtInfo.Rows.Count > 0)
            {
                return GetInstanceFromDB(dtInfo.Rows[0]);
            }
            return null;
        }

        public static TruckInfo GetInstanceFromDB(DataRow row)
        {
            DateTime dateTemp;
            TruckInfo info = new TruckInfo();

            info.ID = row["ID"].ToString(); ;
            info.TRUCKNO = row["TRUCKNO"].ToString();
            info.DRIVERID = row["DRIVERID"].ToString();
            info.DRIVERNAME = row["DRIVERNAME"].ToString();
            info.JSZ_NAME = row["JSZ_NAME"].ToString();
            info.JSZ_NO = row["JSZ_NO"].ToString();
            if (DateTime.TryParse(row["JSZ_YXQ"].ToString(), out dateTemp))
                info.JSZ_YXQ = dateTemp;
            if (DateTime.TryParse(row["XSZ_CT_YXQ"].ToString(), out dateTemp))
                info.XSZ_CT_YXQ = dateTemp;
            if (DateTime.TryParse(row["XSZ_CG_YXQ"].ToString(), out dateTemp))
                info.XSZ_CG_YXQ = dateTemp;
            if (DateTime.TryParse(row["YSZ_CT_SYJL"].ToString(), out dateTemp))
                info.YSZ_CT_SYJL = dateTemp;
            if (DateTime.TryParse(row["YSZ_CG_SYJL"].ToString(), out dateTemp))
                info.YSZ_CG_SYJL = dateTemp;
            if (DateTime.TryParse(row["JYBG_YXQ"].ToString(), out dateTemp))
                info.JYBG_YXQ = dateTemp;
            info.JYBG_JYDW = row["JYBG_JYDW"].ToString();

            info.CYZGZ_JSYZ.ID = row["CYZGZ_JSYZ_ID"].ToString();
            if (DateTime.TryParse(row["CYZGZ_JSYZ_JYJL"].ToString(), out dateTemp))
                info.CYZGZ_JSYZ.JXJYJL = dateTemp;
            if (DateTime.TryParse(row["CYZGZ_JSYZ_CXKH"].ToString(), out dateTemp))
                info.CYZGZ_JSYZ.CXKH = dateTemp;
            if (DateTime.TryParse(row["CYZGZ_JSYZ_YXQ"].ToString(), out dateTemp))
                info.CYZGZ_JSYZ.YXQ = dateTemp;

            info.CYZGZ_YYYZ.ID = row["CYZGZ_YYYZ_ID"].ToString();
            if (DateTime.TryParse(row["CYZGZ_YYYZ_YXQ"].ToString(), out dateTemp))
                info.CYZGZ_YYYZ.YXQ = dateTemp;

            if (DateTime.TryParse(row["MHQ_YXQ"].ToString(), out dateTemp))
                info.MHQ_YXQ = dateTemp;
            double dWeight = 0;
            if (double.TryParse(row["HD_WEIGHT"].ToString(), out dWeight))
                info.HD_WEIGHT = dWeight;

            return info;
        }

        public static bool SaveToDB(TruckInfo info, ref string id)
        {
            try
            {
                if (!string.IsNullOrEmpty(info.ID))//修改
                {
                    DataAccess.ExecuteNonQuery("insert into T_TRUCK_INFO_LOG(ID,UPDATEMAN,UPDATEDATE,LX,TRUCKID,TRUCKNO,DRIVERID,DRIVERNAME,LOG,ORGCODE) select :ID,:UPDATEMAN,Sysdate,3,ID,TRUCKNO,DRIVERID,DRIVERNAME,'修改',ORGCODE from T_TRUCK_INFO where ID=:TRUCKID",
                        new OracleParameter(":ID", Guid.NewGuid().ToString()), new OracleParameter(":UPDATEMAN", Session.CurrentUserName), new OracleParameter(":TRUCKID", info.ID));
                    DataAccess.ExecuteNonQuery("delete from T_TRUCK_INFO where ID = '" + info.ID + "'");
                }
                else//新增
                {
                    info.ID = Guid.NewGuid().ToString();
                    DataAccess.ExecuteNonQuery("insert into T_TRUCK_INFO_LOG(ID,UPDATEMAN,UPDATEDATE,LX,TRUCKID,TRUCKNO,DRIVERID,DRIVERNAME,LOG,ORGCODE) values(:ID,:UPDATEMAN,Sysdate,1,:TRUCKID,:TRUCKNO,:DRIVERID,:DRIVERNAME,'新增',:ORGCODE)",
                        new OracleParameter(":ID", Guid.NewGuid().ToString()), new OracleParameter(":UPDATEMAN", Session.CurrentUserName), new OracleParameter(":TRUCKID", info.ID), new OracleParameter(":TRUCKNO", GetStr(info.TRUCKNO)),
                        new OracleParameter(":DRIVERID", GetStr(info.DRIVERID)), new OracleParameter(":DRIVERNAME", GetStr(info.DRIVERNAME)), new OracleParameter(":ORGCODE", Session.CurrentOrgCode));
                }
                id = info.ID;
                string strSQL = "insert into T_TRUCK_INFO(ID,TRUCKNO,DRIVERID,DRIVERNAME,JSZ_NAME,JSZ_NO,JSZ_YXQ,XSZ_CT_YXQ,XSZ_CG_YXQ,YSZ_CT_SYJL,YSZ_CG_SYJL,CYZGZ_JSYZ_ID,CYZGZ_JSYZ_JYJL,CYZGZ_JSYZ_CXKH,CYZGZ_JSYZ_YXQ,CYZGZ_YYYZ_ID,CYZGZ_YYYZ_YXQ,JYBG_YXQ,JYBG_JYDW,HD_WEIGHT,MHQ_YXQ,ORGCODE)" +
                    " values(:ID,:TRUCKNO,:DRIVERID,:DRIVERNAME,:JSZ_NAME,:JSZ_NO,:JSZ_YXQ,:XSZ_CT_YXQ,:XSZ_CG_YXQ,:YSZ_CT_SYJL,:YSZ_CG_SYJL,:CYZGZ_JSYZ_ID,:CYZGZ_JSYZ_JYJL,:CYZGZ_JSYZ_CXKH,:CYZGZ_JSYZ_YXQ,:CYZGZ_YYYZ_ID,:CYZGZ_YYYZ_YXQ,:JYBG_YXQ,:JYBG_JYDW,:HD_WEIGHT,:MHQ_YXQ,:ORGCODE)";
                DataAccess.ExecuteNonQuery(strSQL, new OracleParameter(":ID", info.ID),
                    new OracleParameter(":TRUCKNO", GetStr(info.TRUCKNO).ToUpper()),
                    new OracleParameter(":DRIVERID", GetStr(info.DRIVERID)),
                    new OracleParameter(":DRIVERNAME", GetStr(info.DRIVERNAME)),
                    new OracleParameter(":JSZ_NAME", GetStr(info.JSZ_NAME)),
                    new OracleParameter(":JSZ_NO", GetStr(info.JSZ_NO)),
                    new OracleParameter(":JSZ_YXQ", info.JSZ_YXQ) { DbType = DbType.Date },
                    new OracleParameter(":XSZ_CT_YXQ", info.XSZ_CT_YXQ) { DbType = DbType.Date },
                    new OracleParameter(":XSZ_CG_YXQ", info.XSZ_CG_YXQ) { DbType = DbType.Date },
                    new OracleParameter(":YSZ_CT_SYJL", info.YSZ_CT_SYJL) { DbType = DbType.Date },
                    new OracleParameter(":YSZ_CG_SYJL", info.YSZ_CG_SYJL) { DbType = DbType.Date },
                    new OracleParameter(":CYZGZ_JSYZ_ID", GetStr(info.CYZGZ_JSYZ.ID)),
                    new OracleParameter(":CYZGZ_JSYZ_JYJL", info.CYZGZ_JSYZ.JXJYJL) { DbType = DbType.Date },
                    new OracleParameter(":CYZGZ_JSYZ_CXKH", info.CYZGZ_JSYZ.CXKH) { DbType = DbType.Date },
                    new OracleParameter(":CYZGZ_JSYZ_YXQ", info.CYZGZ_JSYZ.YXQ) { DbType = DbType.Date },
                    new OracleParameter(":CYZGZ_YYYZ_ID", GetStr(info.CYZGZ_YYYZ.ID)),
                    new OracleParameter(":CYZGZ_YYYZ_YXQ", info.CYZGZ_YYYZ.YXQ) { DbType = DbType.Date },
                    new OracleParameter(":JYBG_YXQ", info.JYBG_YXQ) { DbType = DbType.Date },
                    new OracleParameter(":JYBG_JYDW", GetStr(info.JYBG_JYDW)),
                    new OracleParameter(":HD_WEIGHT", info.HD_WEIGHT),
                    new OracleParameter(":MHQ_YXQ", info.MHQ_YXQ) { DbType = DbType.Date },
                    new OracleParameter(":ORGCODE", Session.CurrentOrgCode));
                return true;
            }
            catch
            {
                return false;
            }
        }

        private static string GetStr(string str)
        {
            return str == null ? "" : str;
        }

        #endregion
    }

    /// <summary>
    /// 运输押运人员证
    /// </summary>
    public class YSYYRYZ
    {
        [DisplayName("身份证号")]
        public string ID { get; set; }

        [DisplayName("有效期")]
        public DateTime YXQ { get; set; }
    }

    /// <summary>
    /// 运输驾驶证
    /// </summary>
    public class YSJSZ
    {
        [DisplayName("身份证号")]
        public string ID { get; set; }

        [DisplayName("继续教育记录")]
        public DateTime JXJYJL { get; set; }

        [DisplayName("诚信考核")]
        public DateTime CXKH { get; set; }

        [DisplayName("有效期")]
        public DateTime YXQ { get; set; }
    }
}
